package com.xyht.sca_s.student_manage_system.modules.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsDormitoryType;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.SmsWnsChargeClassificationRelation;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsWnsChargeClassificationRelationBindReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.req.SmsWnsChargeClassificationRelationUnbindReq;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.resp.SmsChargeCategoryDetailResp;
import com.xyht.sca_s.student_manage_system.modules.dormitory.entity.resp.SmsWnsChargeClassificationRelationResp;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsDormitoryTypeMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.mapper.SmsWnsChargeClassificationRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.dormitory.service.SmsWnsChargeClassificationRelationService;
import com.xyht.sca_s.student_manage_system.modules.goods.entity.SmsGoodsClassification;
import com.xyht.sca_s.student_manage_system.modules.goods.mapper.SmsGoodsClassificationMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsChargeCategoryDetail;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsChargeCategoryDetailMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.dormitory.constant.chargeTypeConstant.CHARGE_TYPE_DORMITORY;
import static com.xyht.sca_s.student_manage_system.modules.dormitory.constant.chargeTypeConstant.CHARGE_TYPE_GOODS;

/**
 * <p>
 * 收费标准和分类关联表(--迎新) 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-01-18
 */
@Service
public class SmsWnsChargeClassificationRelationServiceImpl extends ServiceImpl<SmsWnsChargeClassificationRelationMapper, SmsWnsChargeClassificationRelation> implements SmsWnsChargeClassificationRelationService {

    @Resource
    private SmsWnsChargeClassificationRelationMapper chargeClassificationRelationMapper;
    @Resource
    private SmsDormitoryTypeMapper dormitoryTypeMapper;
    @Resource
    private SmsGoodsClassificationMapper goodsClassificationMapper;
    @Resource
    private SmsWnsChargeCategoryDetailMapper chargeCategoryDetailMapper;

    @Override
    public ResponseResult bindCharge(SmsWnsChargeClassificationRelationBindReq bindReq) {
        if (isNullOrEmpty(bindReq.getChargeDetailId(),bindReq.getClassificationId(),bindReq.getType())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsWnsChargeCategoryDetail smsWnsChargeCategoryDetail = chargeCategoryDetailMapper.selectById(bindReq.getChargeDetailId());
        if (isNullOrEmpty(smsWnsChargeCategoryDetail)) {
            return CommonResult.failed(CommonCodeEnum.CHARGE_DETAIL_NOT_EXIST);
        }
        Integer type = bindReq.getType();
        if (type == CHARGE_TYPE_DORMITORY) {
            SmsDormitoryType smsDormitoryType = dormitoryTypeMapper.selectById(bindReq.getClassificationId());
            if (isNullOrEmpty(smsDormitoryType)) {
                return CommonResult.failed(CommonCodeEnum.DORMITORY_TYPE_NOT_EXIST);
            }
        }else if(type == CHARGE_TYPE_GOODS){
            SmsGoodsClassification smsGoodsClassification = goodsClassificationMapper.selectById(bindReq.getClassificationId());
            if (isNullOrEmpty(smsGoodsClassification)) {
                return CommonResult.failed(CommonCodeEnum.GOODS_CLASSIFICATION_NOT_EXIST);
            }
        }else {
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        List<SmsWnsChargeClassificationRelation> relationList = chargeClassificationRelationMapper.selectList(new LambdaQueryWrapper<SmsWnsChargeClassificationRelation>()
                .eq(SmsWnsChargeClassificationRelation::getChargeDetailId, bindReq.getChargeDetailId())
                .eq(SmsWnsChargeClassificationRelation::getClassificationId, bindReq.getClassificationId()));
        if (relationList.isEmpty()) {
            SmsWnsChargeClassificationRelation smsWnsChargeClassificationRelation = new SmsWnsChargeClassificationRelation();
            smsWnsChargeClassificationRelation.setChargeDetailId(bindReq.getChargeDetailId());
            smsWnsChargeClassificationRelation.setClassificationId(bindReq.getClassificationId());
            smsWnsChargeClassificationRelation.setType(bindReq.getType());
            chargeClassificationRelationMapper.insert(smsWnsChargeClassificationRelation);
        }

        return CommonResult.success();
    }

    @Override
    public ResponseResult unbindCharge(SmsWnsChargeClassificationRelationUnbindReq unbindReq) {
        if (isNullOrEmpty(unbindReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsWnsChargeClassificationRelation smsWnsChargeClassificationRelation = chargeClassificationRelationMapper.selectById(unbindReq.getId());
        if (isNullOrEmpty(smsWnsChargeClassificationRelation)) {
            return CommonResult.failed(CommonCodeEnum.CHARGE_BIND_NOT_EXIST);
        }
        chargeClassificationRelationMapper.deleteById(unbindReq.getId());
        return CommonResult.success();
    }

    @Override
    public ResponseResult getBindChargeList(Integer pageNum, Integer pageSize, Integer type) {
        if (isNullOrEmpty(type)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        Page<SmsWnsChargeClassificationRelation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsWnsChargeClassificationRelation> chargeClassificationRelationLambdaQueryWrapper =
                new LambdaQueryWrapper<SmsWnsChargeClassificationRelation>()
                        .eq(SmsWnsChargeClassificationRelation::getType, type)
                        .orderByDesc(SmsWnsChargeClassificationRelation::getCreateTime);
        chargeClassificationRelationMapper.selectPage(page,chargeClassificationRelationLambdaQueryWrapper);
        List<SmsWnsChargeClassificationRelationResp> respList = page.getRecords()
                .stream()
                .map(relation -> {
                    SmsWnsChargeClassificationRelationResp smsWnsChargeClassificationRelationResp = new SmsWnsChargeClassificationRelationResp();
                    SmsChargeCategoryDetailResp smsChargeCategoryDetailResp = new SmsChargeCategoryDetailResp();
                    SmsWnsChargeCategoryDetail smsWnsChargeCategoryDetail = chargeCategoryDetailMapper.selectById(relation.getChargeDetailId());
                    if (!isNullOrEmpty(smsWnsChargeCategoryDetail)) {
                        BeanUtils.copyProperties(smsWnsChargeCategoryDetail,smsChargeCategoryDetailResp);
                    }
                    smsWnsChargeClassificationRelationResp.setChargeDetailInfo(smsChargeCategoryDetailResp);
                    if (type == CHARGE_TYPE_DORMITORY) {
                        SmsDormitoryType smsDormitoryType = dormitoryTypeMapper.selectById(relation.getClassificationId());
                        if (!isNullOrEmpty(smsDormitoryType)) {
                            smsWnsChargeClassificationRelationResp.setClassificationId(smsDormitoryType.getId());
                            smsWnsChargeClassificationRelationResp.setClassificationName(smsDormitoryType.getTypeName());
                        }
                    }else if(type == CHARGE_TYPE_GOODS){
                        SmsGoodsClassification smsGoodsClassification = goodsClassificationMapper.selectById(relation.getClassificationId());
                        if (!isNullOrEmpty(smsGoodsClassification)) {
                            smsWnsChargeClassificationRelationResp.setClassificationId(smsGoodsClassification.getId());
                            smsWnsChargeClassificationRelationResp.setClassificationName(smsGoodsClassification.getClassificationName());
                        }
                    }
                    smsWnsChargeClassificationRelationResp.setId(relation.getId());
                    return smsWnsChargeClassificationRelationResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(respList,(int)page.getTotal());
    }
}
